#include "systemfile.h"
#include <QString>
#include <stdio.h>
#include <QDataStream>
#include <QtGui>
#include <QDebug>
#include <QMap>
systemFile::systemFile()
{
    QString fileName=QString("%1/settings.ini").arg(QDir::currentPath());
    this->devSetting = new QSettings(fileName,QSettings::IniFormat);
    if(QFile::exists(fileName) == false)
    {
        this->resetSetting();
        if(!this->devSetting->contains("DEVICE"))
        {
            qDebug()<<"devSetting no DEVICE";
        }
        if(!this->devSetting->contains("SERVER"))
        {
            qDebug()<<"devSetting no SERVER";
        }
        if(!this->devSetting->contains("CAMERA"))
        {
            qDebug()<<"devSetting no CAMERA";
        }
        if(!this->devSetting->contains("MODEL"))
        {
            qDebug()<<"devSetting no MODEL";
        }   
    }    
    //读取MAC地址
    this->mac =getHostMacAddress();
    qDebug() <<"=========sysInfo Begin=========="<<this->mac;
    this->readSetting();

}

QString systemFile::getHostMacAddress()
{
    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();// 获取所有网络接口列表
    int nCnt = nets.count();
    QString strMacAddr = "gzjd6269768";
    for(int i = 0; i < nCnt; i ++)
    {
        // 如果此网络接口被激活并且正在运行并且不是回环地址，则就是我们需要找的Mac地址
        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            strMacAddr = nets[i].hardwareAddress();
            break;
        }
    }
    return strMacAddr.remove(":");
}

//读取系统设置
bool systemFile::readSetting()
{
    //
    this->devSetting->setIniCodec(QTextCodec::codecForName("GB2312"));
    // QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    this->devSetting->beginGroup("DEVICE");
    this->galleryName = this->devSetting->value("galleryName").toString();
    this->galleryNumber = this->devSetting->value("galleryNumber").toString();
    this->regNumber = this->devSetting->value("regNumber").toString();
    this->heart = this->devSetting->value("heart").toInt();
    this->path = this->devSetting->value("path").toString();
    this->car_stall=this->devSetting->value("stall").toInt();

    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("SERVER");
    this->serverIp = this->devSetting->value("serverIp").toString();
    this->serverUrl = this->devSetting->value("serverUrl").toString();
    this->updataPicture = this->devSetting->value("updataPicture").toInt();
    this->updataTime = this->devSetting->value("updataTime").toTime();
    this->downTime = this->devSetting->value("downTime").toString();
    this->userName = this->devSetting->value("userName").toString();
    this->passWord = this->devSetting->value("passWord").toString();
    this->datebaseName = this->devSetting->value("datebaseName").toString();
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("CAMERA");
    this->cameraInIp = this->devSetting->value("cameraInIp").toString();
    this->cameraOutIp = this->devSetting->value("cameraOutIp").toString();
    this->savePicture = this->devSetting->value("savePicture").toInt();
    this->vehicleFlag = this->devSetting->value("vehicleFlag").toInt();
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("MODEL");
    this->model = this->devSetting->value("model").toInt();
    this->tempCarIn = this->devSetting->value("tempCarIn").toInt();
    this->passFlag = this->devSetting->value("passFlag").toInt();
    this->first_down = this->devSetting->value("first_down").toInt();
    this->park_type = this->devSetting->value("park_type").toInt();
    this->park_camera = this->devSetting->value("park_camera").toInt();
    this->clearCar= this->devSetting->value("clearCar").toInt();
    this->printFlag=this->devSetting->value("printFlag").toInt();
    this->userFlag=this->devSetting->value("userFlag").toInt();
    this->clearCarDay=this->devSetting->value("clearCarDay").toInt();
    this->FeeTopFlag=this->devSetting->value("FeeTopFlag").toInt();
    this->HandFlag=this->devSetting->value("HandFlag").toInt();
    this->devSetting->endGroup();


    this->devSetting->beginGroup("SPARE");
    this->cameraInIpBei = this->devSetting->value("cameraInIpBei").toString();
    this->cameraOutIpBei = this->devSetting->value("cameraOutIpBei").toString();
    this->mysqlServerIp = this->devSetting->value("mysqlServerIp").toString();
    this->serverIpBei = this->devSetting->value("serverIpBei").toString();
    this->ledIp1 = this->devSetting->value("ledIp1").toString();
    this->ledIp2 = this->devSetting->value("ledIp2").toString();
    this->sqdbPath = this->devSetting->value("sqdbPath").toString();
    this->devSetting->endGroup();

    this->devSetting->beginGroup("FEE");
    this->FreeTime = this->devSetting->value("FreeTime").toString();
    this->FreeTime2 = this->devSetting->value("FreeTime2").toString();
    this->LoseFreeTime = this->devSetting->value("LoseFreeTime").toInt();
    this->AddDayTime = this->devSetting->value("AddDayTime").toInt();
    this->devSetting->endGroup();

  //QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    qDebug()<<"****************************************";
    qDebug()<<"******DEVICE********";
    qDebug()<<"galleryName**"<<this->galleryName;
    qDebug()<<"galleryNumber"<<this->galleryNumber;
    qDebug()<<"regNumber****"<<this->regNumber;
    qDebug()<<"heart********"<<this->heart;
    qDebug()<<"path*********"<<this->path;
    qDebug()<<"passFlag*****"<<this->passFlag;
    qDebug()<<"stall*****"<<this->car_stall;

    qDebug()<<"******SERVER********";
    qDebug()<<"serverIp*****"<<this->serverIp;
    qDebug()<<"serverUrl****"<<this->serverUrl;
    qDebug()<<"updataPicture"<<this->updataPicture;
    qDebug()<<"updataTime***"<<this->updataTime;
    qDebug()<<"downTime*****"<<this->downTime;
    qDebug()<<"userName*****"<<this->userName;
    qDebug()<<"passWord*****"<<this->passWord;
    qDebug()<<"datebaseName*"<<this->datebaseName;
    qDebug()<<"******CAMERA********";
    qDebug()<<"cameraInIp*****"<<this->cameraInIp;
    qDebug()<<"cameraOutIp****"<<this->cameraOutIp;
    qDebug()<<"savePicture****"<<this->savePicture;
    qDebug()<<"vehicleFlag****"<<this->vehicleFlag;
    qDebug()<<"******MODEL********";
    qDebug()<<"model********"<<this->model;
    qDebug()<<"tempCarIn****"<<this->tempCarIn;
    qDebug()<<"passFlag*****"<<this->passFlag;
    qDebug()<<"first_down****"<<this->first_down;
    qDebug()<<"park_type****"<<this->park_type;
    qDebug()<<"park_camera****"<<this->park_camera;
    qDebug()<<"clearCar*******"<<this->clearCar;
    qDebug()<<"printFlag******"<<this->printFlag;
    qDebug()<<"userFlag*******"<<this->userFlag;
    qDebug()<<"clearCarDay****"<<this->clearCarDay;
    qDebug()<<"FeeTopFlag*****"<<this->FeeTopFlag;
    qDebug()<<"HandFlag*******"<<this->HandFlag;

    qDebug()<<"******SPARE*******";
    qDebug()<<"cameraInIpBei*****"<<this->cameraInIpBei;
    qDebug()<<"cameraOutIpBei****"<<this->cameraOutIpBei;
    qDebug()<<"mysqlServerIp*****"<<this->mysqlServerIp;
    qDebug()<<"serverIpBei****"<<this->serverIpBei;
    qDebug()<<"ledIp1****"<<this->ledIp1;
    qDebug()<<"ledIp2****"<<this->ledIp2;
    qDebug()<<"sqdbPath****"<<this->sqdbPath;
    qDebug()<<"****************************************";

    qDebug()<<"******FEE*******";
    qDebug()<<"FreeTime*****"<<this->FreeTime;
    qDebug()<<"FreeTime2****"<<this->FreeTime2;
    qDebug()<<"LoseFreeTime**"<<this->LoseFreeTime;
    qDebug()<<"AddDayTime***"<<this->AddDayTime;
    qDebug()<<"****************************************";
    return true;
}


bool systemFile::SettingSave()
{
    this->devSetting->beginGroup("DEVICE");
    this->devSetting->setValue("galleryName",this->galleryName);
    this->devSetting->setValue("galleryNumber",this->galleryNumber);
    this->devSetting->setValue("regNumber",this->regNumber);
    this->devSetting->setValue("heart",this->heart);
    this->devSetting->setValue("path",this->path);
    this->devSetting->setValue("stall",this->car_stall);
    this->devSetting->endGroup();

    this->devSetting->beginGroup("SERVER");
    this->devSetting->setValue("serverIp",this->serverIp);
    this->devSetting->setValue("serverUrl",this->serverUrl);
    this->devSetting->setValue("updataPicture",this->updataPicture);
    this->devSetting->setValue("updataTime",this->updataTime);
    this->devSetting->setValue("downTime",this->downTime);
    this->devSetting->setValue("userName",this->userName);
    this->devSetting->setValue("passWord",this->passWord);
    this->devSetting->setValue("datebaseName",this->datebaseName );
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("CAMERA");
    this->devSetting->setValue("cameraInIp",this->cameraInIp);
    this->devSetting->setValue("cameraOutIp",this->cameraOutIp);
    this->devSetting->setValue("savePicture",this->savePicture);
    this->devSetting->setValue("vehicleFlag",this->vehicleFlag);
    this->devSetting->endGroup();
    //
    this->devSetting->beginGroup("MODEL");
    this->devSetting->setValue("model",this->model);
    this->devSetting->setValue("tempCarIn",this->tempCarIn);
    this->devSetting->setValue("passFlag",this->passFlag );
    this->devSetting->setValue("first_down",this->first_down);
    this->devSetting->setValue("park_type",this->park_type);
    this->devSetting->setValue("park_camera",this->park_camera);
    this->devSetting->setValue("clearCar",this->clearCar);
    this->devSetting->setValue("printFlag",this->printFlag);
    this->devSetting->setValue("userFlag",this->userFlag);
    this->devSetting->setValue("clearCarDay",this->clearCarDay);
    this->devSetting->setValue("FeeTopFlag",this->FeeTopFlag);
     this->devSetting->setValue("HandFlag",this->HandFlag);
    this->devSetting->endGroup();


    this->devSetting->beginGroup("SPARE");
    this->devSetting->setValue("cameraInIpBei",this->cameraInIpBei);
    this->devSetting->setValue("cameraOutIpBei",this->cameraOutIpBei);
    this->devSetting->setValue("mysqlServerIp",this->mysqlServerIp);
    this->devSetting->setValue("serverIpBei", this->serverIpBei);
    this->devSetting->setValue("ledIp1",this->ledIp1);
    this->devSetting->setValue("ledIp2",this->ledIp2);
    this->devSetting->setValue("sqdbPath",this->sqdbPath);
    this->devSetting->endGroup();

    this->devSetting->beginGroup("FEE");
    this->devSetting->setValue("FreeTime",this->FreeTime);
    this->devSetting->setValue("FreeTime2",this->FreeTime2);
    this->devSetting->setValue("LoseFreeTime",this->LoseFreeTime);
    this->devSetting->setValue("AddDayTime", this->AddDayTime);
    this->devSetting->endGroup();
    this->devSetting->sync();
}


bool systemFile::resetSetting()
{
    QString fileName=QString("%1/settings.ini").arg(QDir::currentPath());
    QString fileInfo;
    QFile myfile(fileName);
    qDebug() << "*********************************";
    qDebug() << "reset setting";
    qDebug() << "*********************************";
    myfile.open(QIODevice::WriteOnly);
    //
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    fileInfo.append("[DEVICE]\r\n");
    fileInfo.append("galleryName =智能停车场\r\n");
    fileInfo.append("galleryNumber = 1\r\n");
    fileInfo.append("regNumber = 00000000\r\n");
    fileInfo.append("heart = 40\r\n");
    fileInfo.append("path = /home/root/\r\n");
    fileInfo.append("stall = 500\r\n");

    //
    fileInfo.append("[SERVER]\r\n");
    fileInfo.append("serverIp = 192.168.10.200\r\n");
    fileInfo.append("serverUrl = test\r\n");
    fileInfo.append("updataPicture = 1\r\n");
    fileInfo.append("updataTime = 01:00:00\r\n");
    fileInfo.append("downTime = 30\r\n");
    fileInfo.append("userName = copy\r\n");
    fileInfo.append("passWord = 123\r\n");
    fileInfo.append("datebaseName = pn\r\n");
    //
    fileInfo.append("[CAMERA]\r\n");
    fileInfo.append("cameraInIp = 192.168.10.223\r\n");
    fileInfo.append("cameraOutIp = 192.168.10.224\r\n");
    fileInfo.append("savePicture = 0\r\n");
    fileInfo.append("vehicleFlag = 0\r\n");
    //
    fileInfo.append("[MODEL]\r\n");
    fileInfo.append("model = 1\r\n");
    fileInfo.append("tempCarIn = 0\r\n");
    fileInfo.append("passFlag = 0\r\n");
    fileInfo.append("first_down = 1\r\n");
    fileInfo.append("park_type = 1\r\n");
    fileInfo.append("park_camera = 1\r\n");
    fileInfo.append("clearCar = 0\r\n");
    fileInfo.append("printFlag = 1\r\n");
    fileInfo.append("userFlag = 1\r\n");
    fileInfo.append("clearCarDay = 1\r\n");
    fileInfo.append("FeeTopFlag = 1\r\n");
    fileInfo.append("HandFlag = 1\r\n");
    //
    fileInfo.append("[SPARE]\r\n");
    fileInfo.append("cameraInIpBei = 127.0.0.1\r\n");
    fileInfo.append("cameraOutIpBei = 127.0.0.1\r\n");
    fileInfo.append("mysqlServerIp = 127.0.0.1\r\n");
    fileInfo.append("serverIpBei = 127.0.0.1\r\n");
    fileInfo.append("ledIp1 = 127.0.0.1\r\n");
    fileInfo.append("ledIp2 = 127.0.0.1\r\n");
    fileInfo.append("sqdbPath = /home/root/\r\n");
    //
    fileInfo.append("[FEE]\r\n");
    fileInfo.append("FreeTime = 30\r\n");
    fileInfo.append("FreeTime2 = 30\r\n");
    fileInfo.append("LoseFreeTime = 0\r\n");
    fileInfo.append("AddDayTime = 1\r\n");
    //
    myfile.write(fileInfo.toLocal8Bit());
    myfile.close();
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    this->devSetting = new QSettings(fileName,QSettings::IniFormat);
    usleep(100000);
    return true;
}



